package com.hongye.broker.controller;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.ObjectUtil;
import com.hongye.broker.service.BrokerService;
import com.hongye.broker.service.CaptchaService;
import com.hongye.broker.util.Des;
import com.hongye.broker.util.PhoneFormatCheckUtils;
import com.hongye.common.entity.BrokerEntity;
import com.hongye.common.exception.ServiceException;
import com.hongye.common.response.ResponseResult;
import jodd.util.StringUtil;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/*
* 经纪人层
* */
@RestController
@RequestMapping("/broker")
public class BrokerController {
    @Autowired
    private BrokerService brokerService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 运算码业务处理对象
     */
    @Autowired
    private CaptchaService captchaService;


    //固定redis的存储空间
    private final String REDIS_PHONE = "redis_phone:";
    private final String REDIS_Token = "redis_token";
    //秘钥
    private final String privateKey = "8844551785185648";

    //查询排行榜
    @RequestMapping("/phb")
    public ResponseResult phb(String brokerName) {
        List<BrokerEntity> list = brokerService.phb(brokerName);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        return new ResponseResult(200, "排行榜", map);
    }

    /*
     *获取验证码
     */
    @RequestMapping("/sendCode")
    public ResponseResult sendCode(@RequestParam String phone/*,
                                   @RequestParam String x,
                                   @RequestParam String y*/) {
        /*
        * 滑动验证码是否生效
        * */
        /*Boolean result = captchaService.checkVerificationResult(x, y);
        if (result == false){
            throw new ServiceException("500","活动验证码验证失败,全新发送失败");
        }*/


        if (StringUtil.isEmpty(phone)) {
            throw new ServiceException("401", "手机号不能为空");
        }
        if (!PhoneFormatCheckUtils.isPhoneLegal(phone)) {
            throw new ServiceException("401", "手机号格式不对");
        }
        //生成四位数验证码
        String code = RandomStringUtils.random(6, false, true);
        //判断redis是否存在
        if (redisTemplate.hasKey(REDIS_PHONE + phone)) {
            throw new ServiceException("402", "3分钟内不能重复发短信");
        }
        redisTemplate.opsForValue().set(REDIS_PHONE + phone, code, 3, TimeUnit.MINUTES);
        HashMap<String, String> map = new HashMap<>();
        map.put("phone", phone);
        map.put("code", code);

        try {
            rabbitTemplate.convertAndSend("phone.queue", map);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(-1, "发送失败,请重新发送");
        }
        return new ResponseResult(1, "发送成功");
    }

    //登录
    @RequestMapping("/brokerLogin")
    public ResponseResult brokerLogin(@RequestParam String phone, @RequestParam String code) {
        BrokerEntity brokerEntity = brokerService.findBrokerPhone(phone);
        if (ObjectUtil.isNull(brokerEntity) && brokerEntity.getStatus() != 1) {
            throw new ServiceException("401", "手机号异常");
        }
        //取出
        String s = redisTemplate.opsForValue().get(REDIS_PHONE + phone);
        if (!s.equals(code)) {
            throw new ServiceException("401", "验证码错误");
        }

        String token = UUID.randomUUID().toString();
        //加密
        String pho = Des.encryptjm(phone, privateKey);

        //存储redis
        redisTemplate.opsForValue().set(REDIS_Token + token, brokerEntity.toString(), 5, TimeUnit.DAYS);

        HashMap<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("phone", pho);
        return ResponseResult.ok(1, "登录成功", map);
    }

    //首页
    @RequestMapping("/brokenOnePage")
    public ResponseResult brokenOnePage(@RequestParam String phone) {
        //解析验证码
        String pho = Des.decryptjm(phone, privateKey);
        BrokerEntity brokerEntity = brokerService.findBrokerPhone(pho);

        if (ObjectUtil.isNull(brokerEntity) && brokerEntity.getStatus() != 1) {
            throw new ServiceException("401", "手机号异常");
        }

        //返回首页内容
        ResponseResult responseResult = brokerService.brokerLogin(brokerEntity);
        return responseResult;
    }

}
